<?php
class User{

	private $user_data;

	public function authenticate($email,$password,$penca){
		$email = addslashes($email);
		$password = addslashes($password);
		$penca = addslashes($penca);

		$sql = "SELECT u.id_usuario, u.alias, u.email, u.password, u.bloqueado, z.zona, z.codigo, p.id_penca, p.nombre, u.activado, (u.id_usuario = p.id_usuario) adm
				FROM usuarios u, pencas  p, zonashorarias z, usuariospenca up 
				WHERE u.email = '$email' 
				AND p.nombre= '$penca'
				AND u.password = MD5('$password') 
				AND u.id_usuario = up.id_usuario 
				AND up.id_penca = p.id_penca 
				AND z.id_zona = u.id_zonahoraria 
				AND u.borrado != 1 
				AND p.borrada != 1
				LIMIT 1";


		$config = Zend_Registry::get('config');
		$db = Zend_Db::factory($config->database->adapter,$config->database->params);

		$db->setFetchMode(Zend_Db::FETCH_OBJ);
		$result = $db->fetchAll($sql);


		if(count($result)==1) {
			$row = $result[0];

			if($row->activado!=1) return 4;
			else if($row->bloqueado==1) return 3;
			else{
				$this->user_data = $row;
				return 0;
			}
		}
		else{
			return 2;
		}

	}

	public function getUserId(){
		return $this->user_data->id_usuario;
	}

	public function getPencaId(){
		return $this->user_data->id_penca;
	}

	public function isAdminPenca(){
		return $this->user_data->adm;
	}

	public function getUserData(){
		return $this->user_data;
	}
	
	public function getUserTimeDif(){
		list($horas,$minutos) = explode(':',date('P'));
		$codigo_horario = $this->user_data->zona - ($horas + $minutos/60);
		return $codigo_horario;
	}
	
	public function getUTC(){
		return $this->user_data->codigo;
	
	}
	
	
}